<template>
    <div class="list" ref="wrapper">
        <div>
        <div class="area">
            <div class="title border-topbottom">当前城市</div>
            <div class="button-list">
                <div class="button-warper">
                    <div class="button">{{this.$store.state.city}}</div>
                </div>
            </div>
        </div>
        <div class="area">
            <div class="title border-topbottom">热门城市</div>
            <div class="button-warper" 
                 v-for="item in hot" :key="item.id"
                 @click="handleCityClick(item.name)">
                    <div class="button">{{item.name}}</div>
                </div>
        </div>
        <div class="area"
             v-for="(item, key) in cities" :key='key'
             :ref="key">
            <div class="title border-topbottom" >{{key}}</div>
                <div class="item-list border-bottom" 
                     v-for="list in item" :key="list.id"
                     @click="handleCityClick(list.name)">
                    <div class="item">{{list.name}}</div>
                </div>
            
        </div>
        </div>
    </div>
</template>

<script>
import BScroll from 'better-scroll'   //滚动插件 只处理包裹的第一个子元素使他滚动 
export default {
  name: 'citylist',
  props:{
      hot:Array,
      cities:Object,
      zimu:String,
  },
  methods:{
      handleCityClick(city){
         this.$store.dispatch('changeCity',city)  //vuex数据传递 在点击热门城市对应当前城市
         //编程式导航,router.push()方法返回路由
         this.$router.push('/')                   //组件用dispatch方法调用actions  
      }                                        //数据很少可以不通过action中转，这可以直接调用commit方法，省略actions
  },                                                                    
  mounted(){
      this.scroll = new BScroll(this.$refs.wrapper)
  },
  watch: {
      zimu(){
         if(this.zimu){
             const element=this.$refs[this.zimu][0]  //v-for得到的数组 ,如果想获取该ref时需要加 [0]，如this.$refs[refsArrayItem] [0]
             this.scroll.scrollToElement(element)  //指定滑到某个元素的位置
         }
      }
  },

}
</script>

<style scoped>
    .list{
        overflow: hidden;
        position: absolute;
        top: 1.6rem;
        right: 0;
        bottom: 0;
        left: 0;;
    }
    .area{ 
        overflow: hidden;
    }
    .title{
        height: .44rem;
        line-height: .44rem;
        background: #eee;
        padding-left: .2rem;
        color: #777;
        font-size: .26rem;
    }
    .button-list{
        padding: .1rem .6rem .1rem .1rem;
        height: 0;
        
    }
    .button-warper{
        float: left;
        width: 33.33%;
    }
    .button-warper>.button{
        padding: .1rem;
        text-align: center;
        border: .02rem solid #ccc;
        border-radius: .06rem;
        margin: .12rem .2rem .12rem .2rem;
    }
    .item-list>.item{
        padding-left: .12rem;
        line-height: .6rem;
    }
    
</style>